package com.wxtt.android.view;

import android.content.Context;
import android.view.View;

import com.fast.dev.frame.utils.AndroidInfoUtils;
import com.fast.dev.frame.utils.UIUtils;
import com.wxtt.android.R;
import com.wxtt.android.iterfaces.I_Refresh;

import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * 说明：下拉刷新，加载更多控件(废弃)
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/1/25 10:20
 * <p/>
 * 版本：verson 1.0
 */
public class PullRefreshView implements BGARefreshLayout.BGARefreshLayoutDelegate {

    //是否加载更多
    private boolean isLoadMoreEnable = false;
    //需要刷新的View
    private View refreshView;
    //刷新监听器
    private I_Refresh listener;
    //刷新控件
    private BGARefreshLayout bga;
    private Context mContext;
    /****刷新状态****/
    //正在刷新
    public final static int STATE_ON_REFRESH = 0;
    //正在加载更多
    public final static int STATE_ON_LOADMORE = 1;
    //没有加载
    public final static int STATE_NO_REFRESH = 2;

    //当前状态
    private int currentState = STATE_NO_REFRESH;

    /**
     * 说明构造方法
     */
    public PullRefreshView(BGARefreshLayout bga,View view,I_Refresh listener){
        this(bga,view,false,listener);
    }

    /**
     * 说明构造方法
     */
    public PullRefreshView(BGARefreshLayout bga,View view,boolean isLoadMoreEnable,I_Refresh listener){
        this.refreshView = view;
        this.listener = listener;
        this.bga = bga;
        this.isLoadMoreEnable = isLoadMoreEnable;
        onInit();
    }

    /**
     * 说明：初始化
     */
    private void onInit() {
        if (bga == null){
            throw new RuntimeException("BGARefreshLayout is null");
        }
        if (refreshView == null){
            throw new RuntimeException("refreshView is null");
        }
        mContext = refreshView.getContext();
        //设置刷新代理
        bga.setDelegate(this);
        //设置刷新头
        BGANormalRefreshViewHolder holder = new BGANormalRefreshViewHolder(mContext,isLoadMoreEnable);
        holder.setLoadingMoreText(UIUtils.getString(R.string.loading_refresh_more));
//        bga.setIsShowLoadingMoreView(isLoadMoreEnable);
        bga.setRefreshViewHolder(holder);
    }

    /**
     * 说明：自动刷新
     */
    public void autoRefresh(){
        if (currentState == STATE_NO_REFRESH){
            bga.beginRefreshing();
        }
    }

    /**
     * 说明：自动刷新更多
     */
    public void autoRefreshMore(){
        if (currentState == STATE_NO_REFRESH){
            bga.beginLoadingMore();
        }
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        if (AndroidInfoUtils.isNetConnected(mContext)){
            if (listener != null){
                if (currentState != STATE_ON_REFRESH){
                    currentState = STATE_ON_REFRESH;
                    listener.onRefesh();
                }
            }
        }else {
            if (listener != null){
                listener.onRefreshNotNetwork();
            }
            currentState = STATE_NO_REFRESH;
            //结束刷新
            onRefreshEnd();
        }
    }

    /**
     * 说明：结束下拉刷新
     */
    public void onRefreshEnd(){
        currentState = STATE_NO_REFRESH;
        bga.endRefreshing();
    }

    /**
     * 说明：结束加载更多刷新
     * @param isShowLoadMore 是否显示加载更多
     */
    public boolean onLoadMoreEnd(boolean isShowLoadMore){
        currentState = STATE_NO_REFRESH;
        bga.endLoadingMore();
        return isShowLoadMore;
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        if (AndroidInfoUtils.isNetConnected(mContext)){
            if (listener != null){
                if (currentState != STATE_ON_LOADMORE){
                    currentState = STATE_ON_LOADMORE;
                    listener.onLoadMore();
                }
            }
        }else {
            if (listener != null){
                listener.onRefreshNotNetwork();
            }
            currentState = STATE_NO_REFRESH;
            //结束刷新
            onLoadMoreEnd(true);
        }
        return true;
    }

    /**
     * 说明：是否正在刷新
     * @return
     */
    public boolean isRefresh(){
        return currentState == STATE_ON_REFRESH;
    }

    /**
     * 说明：是否正在加载更多
     * @return
     */
    public boolean isLoadingMore(){
        return currentState == STATE_ON_LOADMORE;
    }
}
